var action;
var commitFieldButton;
var deleteFieldButton;
var fieldID;
var fieldName;
var fieldPosition;
var fieldLength;
var fieldType;
var fieldDescription;
var fieldPrimary;
var url;
var i =0;

jQuery(function() {

    //
    //jQuery('button[name="field_commit"]').each(function(index, element) 
    jQuery('button[name="field_commit"]').each(function(index, element) 
    {
        i++;
        jQuery(this).attr('id', 'field_commit_' + i);
        jQuery(getFieldRow(this)).attr('id', 'field_row_' + i);
        evaluateFieldRow(getFieldRow(this))   ;
	
    });
	
});

function getFieldRow(element){
    
    return jQuery(element).closest('tr');
    
}
function verifyFieldRow(fieldRow)
{


    //jQuery(fieldRow).attr("id", i);
    
    fieldID = jQuery('input[name="field_id"]', fieldRow).val();
    
    fieldName = jQuery('input[name="field_name"]', fieldRow).val();
    fieldPosition = jQuery('input[name="field_position"]', fieldRow).val();
    fieldLength = jQuery('input[name="field_length"]', fieldRow).val();
    fieldType =jQuery('input[name="field_type"]', fieldRow).val();
    fieldDescription = jQuery('input[name="field_description"]', fieldRow).val();
    
    
    if(fieldID == "")
    {
        action = 'FIELD_ADD';
    }
    else{
        action = 'FIELD_UPDATE';
    }

    if(fieldName == "")
    {
        alert('Field Name should not be empty');
        return false;
    }
    if(fieldPosition == "")
    {
        alert('Field Name should not be empty');
        return false;
    }
    if(fieldLength == "")
    {
        alert('Field Name should not be empty');
        return false;
    }
    if(fieldType == "")
    {
        alert('Field Name should not be empty');
        return false;
    }
    if(fieldDescription == "")
    {
        alert('Field Name should not be empty');
        return false;
    }
    if(jQuery('input[name="field_primary"]', fieldRow).is(':checked')){
        fieldPrimary = 1;    
    }
    else{
        fieldPrimary = 0;
    }
    return true;
}

function evaluateFieldRow(fieldRow)
{       

    
    jQuery('button[name="field_commit"]', fieldRow).click(function() {

        //alert(jQuery(fieldRow).attr('id'));
        verifyFieldRow(fieldRow);
        commitFieldButton = this;
        if(!verifyFieldRow(fieldRow)){
            return false;
        }
        jQuery.ajax( 
        {
            url : '?method=' + action,
            type : "POST",
            data :{
                'field_id'          : fieldID,
                'field_name'        : fieldName,
                'field_position'    : fieldPosition,
                'field_length'      : fieldLength,
                'field_type'        : fieldType,
                'field_description' : fieldDescription,
                'field_primary'     : fieldPrimary,
                'table_id'           : jQuery('#table_id').val()
                
                   
            }
            , 
            dataType : "xml",
            success : function(xml) {
                alert(jQuery("message", xml).text());
                var fieldID = jQuery("field_id", xml).text();
                if(action == "FIELD_ADD"){
                    jQuery('input[name="field_id"]', getFieldRow(commitFieldButton)).val(fieldID);
                }
                else if(action == "FIELD_UPDATE"){
            //TODO
            }
            }
        });  
                
    });

    
    jQuery('button[name="field_delete"]', fieldRow).click(function() {
        fieldID = jQuery('input[name="field_id"]', fieldRow).val();
        deleteFieldButton = this;
        jQuery.ajax( 
        {
            url : '?method=FIELD_DELETE',
            type : "POST",
            data :{
                'field_id'          : fieldID
            }
            , 
            dataType : "xml",
            success : function(xml) {
                alert(jQuery("message", xml).text());
                if(jQuery("success", xml).text() == 1){
                    jQuery(getFieldRow(deleteFieldButton)).remove();
                }
               
            }
        }); 
        return false;
                
    });

    
}